<template>
  <div class="wrap">
    <div class="show-title">角色ID：{{ role_id }}</div>
    <div class="show-title">角色名称：{{ role_name }}</div>
    <el-form
      ref="ruleFormRef"
      style="max-width: 600px"
      :model="ruleForm"
      status-icon
      :rules="rules"
      label-width="auto"
      class="ruleForm-wrap"
    >
      <el-form-item label="角色名称" prop="name">
        <el-input v-model="ruleForm.name" />
      </el-form-item>

      <el-form-item class="btn-wrap">
        <el-button type="primary" @click="submitForm(ruleFormRef)">
          修改
        </el-button>
        <el-button type="warning" plain @click="resetForm(ruleFormRef)">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup>
import { reactive, ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { postData } from '@/http/request.js'
import { useRouter, useRoute } from 'vue-router'
let router = useRouter()
let route = useRoute()

let ruleFormRef = ref(null)
const role_id = route.query.role_id
const role_name = route.query.role_name

const ruleForm = reactive({
  name: '',
})
const rules = reactive({
  name: [{ required: true, message: '', trigger: 'blur' }],
})

const submitForm = async (formEl) => {
  if (!formEl) return
  if (ruleForm.name.length == 0) {
    ElMessage({
      type: 'error',
      message: '请输入要修改的名称',
    })
    return
  }
  console.log()
  await formEl.validate((valid, fields) => {
    if (valid) {
      postData('/role/update', {
        role_id: role_id,
        role_name: ruleForm.name,
      })
        .then((res) => {
          if (res.code == 0) {
            ElMessage({
              type: 'success',
              message: '修改成功',
            })
            setTimeout(() => {
              router.push('/roleList')
            }, 3000)
          } else {
            ElMessage({
              type: 'error',
              message: res,
            })
          }
        })
        .catch((err) => {
          ElMessage({
            type: 'error',
            message: '请稍后重试',
          })
        })
    } else {
      ElMessage({
        type: 'error',
        message: fields,
      })
    }
  })
}

const resetForm = (formEl) => {
  if (!formEl) return
  formEl.resetFields()
}

onMounted(() => {})
</script>
<style scope>
.wrap {
  margin: 20px 0 0 20px;
}

.title {
  width: 100%;
  height: 40px;
  line-height: 40px;
  position: relative;
  /* background: #c0dee2; */
  padding-left: 30px;
  font-size: 14px;
  margin: 6px 0 20px 0;
  /* border-radius: 12px 12px 0 0; */
  /* font-weight: bold; */
}

.title::before {
  content: '';
  width: 2px;
  height: 16px;
  background: #676a6d;
  position: absolute;
  top: 12px;
  left: 15px;
}

.title span {
  float: left;
  color: #676a6d;
  margin-left: 25px;
}
.show-title {
  height: 30px;
  line-height: 30px;
  color: #676a6d;
  font-size: 14px;
}
.btn-wrap {
  margin-top: 30px;
}
.ruleForm-wrap{
  margin-top:20px;
}
</style>
